package ngames.com.sushitv.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.action.VideoRankListAction;
import ngames.com.sushitv.temp.Adapter.MyBannerAdapter;
import ngames.com.sushitv.temp.Cache.BitmapCache;
import ngames.com.sushitv.temp.Class.FanMade;
import ngames.com.sushitv.temp.Interface.FanMadeInterface;
import ngames.com.sushitv.temp.Module.FanMadeFactory;
import ngames.com.sushitv.action.FanMadeAction;
import ngames.com.sushitv.temp.Thread.FanMadeBannerThread;

/**
 * Created by edmond on 16-7-26.
 */
public class FanMadeFragment extends Fragment implements FanMadeInterface {
    View view;

    private ViewPager viewPager;
    private SwipeRefreshLayout swipeRefreshLayout;

    private List<ImageView> list;
    private LinearLayout linearLayout;

    private List<String> imageUrls = new ArrayList<>();

    private MyBannerAdapter adapter;
    private BannerListener listener;

    private int pointIndex = 0;

    private FanMadeAction fanMadeAction;

    private AppApplication app = null;

    private FanMadeBannerThread thread;

    private Activity activity;

    private boolean isInit = false;

    VideoRankListAction vRa;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_fan_made,container,false);
        activity = getActivity();

        FanMadeFactory fanmadeFactory = new FanMadeFactory();
        fanMadeAction = new FanMadeAction(getActivity(),view,fanmadeFactory,this);
        fanMadeAction.onCreate();
        app = (AppApplication) getActivity().getApplication();
        if(app.queue==null){
            app.queue = Volley.newRequestQueue(getActivity());
        }

        if(app.imageLoader==null){
            app.imageLoader = new ImageLoader(app.queue,new BitmapCache());
        }

        findViews();
        initData();
        initAction();

        vRa=new VideoRankListAction(getActivity(),view,R.id.recycler_fanMade_top,2,6+"","O_D");
        vRa.onCreate();

        return view;
    }

    private void findViews(){
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        linearLayout = (LinearLayout) view.findViewById(R.id.points);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);


    }

    private void initAction(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                fanMadeAction.refreshData();
            }
        });
    }

    private void initData(){
        fanMadeAction.initData();
    }

    private void setBanner(List<String> imageUrlsArray){
        if(isInit){
            viewPager.setOnPageChangeListener(null);
            linearLayout.removeAllViews();
            thread.setStop();
        }

        thread = new FanMadeBannerThread(activity,viewPager,5000);

        imageUrls = imageUrlsArray;
        list = new ArrayList<>();
        View view;
        LinearLayout.LayoutParams params;

        Log.d("FeaturedFragment", String.valueOf(imageUrls.get(0)+"fdsf"));

        for(int i=0;i<imageUrls.size();i++){
            NetworkImageView imageView = new NetworkImageView(getActivity());
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            imageView.setDefaultImageResId(R.mipmap.ic_launcher);
            imageView.setErrorImageResId(R.mipmap.ic_launcher);
            Log.d("FeaturedFragment", String.valueOf(imageUrls.size()));
            imageView.setImageUrl(imageUrls.get(i),app.imageLoader);
            list.add(imageView);

            view = new View(getActivity());
            params = new LinearLayout.LayoutParams(20,20);
            params.leftMargin = 10;
            view.setBackgroundResource(R.drawable.point_background);
            view.setLayoutParams(params);
            view.setEnabled(false);

            linearLayout.addView(view);
        }

        adapter = new MyBannerAdapter(list);
        viewPager.setAdapter(adapter);
        listener = new BannerListener();

        viewPager.setOnPageChangeListener(listener);
        int index = (Integer.MAX_VALUE/2) - (Integer.MAX_VALUE/2%list.size());
        viewPager.setCurrentItem(1000);
        Log.d("Featured", String.valueOf(pointIndex));
        thread.start();
        Log.d("pointIndex", String.valueOf(pointIndex));
        linearLayout.getChildAt(pointIndex).setEnabled(true);
    }

    @Override
    public void initingData(List<String> imageUrls, List<FanMade> featuredArray) {
        setBanner(imageUrls);
    }

    @Override
    public void initedData() {
        
    }

    @Override
    public void refresinghData() {

    }


    @Override
    public void refreshedData(List<String> imageUrls, List<FanMade> featuredArray) {
        setBanner(imageUrls);
        Log.d("FeaturedFragment","FreshEd");
        swipeRefreshLayout.setRefreshing(false);
    }

    class BannerListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {
            int newPosition = position % imageUrls.size();
            if(linearLayout.getChildAt(pointIndex)!=null){
                linearLayout.getChildAt(newPosition).setEnabled(true);
                linearLayout.getChildAt(pointIndex).setEnabled(false);
            }
            pointIndex = newPosition;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}
